#include "balloonVision.h"
#include <math.h>

int getTargets(IplImage* src, Balloon targets[], CvPoint origin) {

	IplImage* image_hsv = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
	IplImage* dest = cvCloneImage(src);

	int n_g = detection(src, dest, image_hsv, targets, hsv_min_g, hsv_max_g,
			hsv_min2_g, hsv_max2_g, origin);

	//Rilascio delle risorse
	cvReleaseImage(&image_hsv);
	cvReleaseImage(&dest);

	return n_g;
}

int getObstacles(IplImage* src, Balloon obstacles[], CvPoint origin) {

	IplImage* image_hsv = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
	IplImage* dest = cvCloneImage(src);

	int n_a = detection(src, dest, image_hsv, obstacles, hsv_min_a, hsv_max_a,
			hsv_min2_a, hsv_max2_a, origin);

	//Rilascio delle risorse
	cvReleaseImage(&image_hsv);
	cvReleaseImage(&dest);

	return n_a;
}

void drawEllispe(IplImage* dest, int n, Balloon balloons[],
		CvScalar colorEllipse) {
	for (int i = 0; i < n; i++) {
		cvEllipseBox(dest, balloons[i].ellipse, colorEllipse, 3, CV_AA, 0);
		cvLine(dest, cvPoint(dest->width / 2, dest->height), cvPoint(
				balloons[i].ellipse.center.x, balloons[i].ellipse.center.y),
				cvScalar(0, 0, 0), 2);
	}
}

int nearest(Balloon balloons[], int riemp) {
	int indice = 0;
	float area_max = balloons[0].area;
	float area_curr;

	int dist_min = balloons[0].distance;
	int dist_curr;
	if (riemp != 0) {
		for (int i = 0; i < riemp; i++) {
			dist_curr = balloons[i].distance;
			area_curr = balloons[i].area;
			if (dist_curr < dist_min) {
				dist_min = dist_curr;
				area_max = area_curr;
				indice = i;
			} else if (dist_curr == dist_min) {
				if (area_curr > area_max) {
					dist_min = dist_curr;
					area_max = area_curr;
					indice = i;
				} else if (area_curr == area_max) {
					if (fabs(balloons[i].angle) < fabs(balloons[indice].angle))
						dist_min = dist_curr;
					area_max = area_curr;
					indice = i;
				}
			}
		}
	} else {
		indice = -1;
	}

	printf("Indice =%d\nGradi =%f\n", indice, balloons[indice].angle);
	return indice;
}

IplImage* connectToCam(int initial) {

	CURL* session = curl_easy_init();
	IplImage* frame = NULL;
	char filename[100];
	FILE* file;

	sprintf(filename, "./Curr.jpg");
	file = fopen(filename, "w");

	if(initial == 1){
		curl_easy_setopt(session,CURLOPT_URL,"http://root:visione@192.168.43.201/axis-cgi/com/ptz.cgi");
		curl_easy_setopt(session,CURLOPT_POSTFIELDS,"move=home");
		curl_easy_perform(session);


	}
	curl_easy_setopt(session,CURLOPT_URL,"http://192.168.43.201/jpg/image.jpg");
	curl_easy_setopt(session,CURLOPT_POSTFIELDS,"resolution=CIF");
	curl_easy_setopt(session,CURLOPT_WRITEDATA,file);
	curl_easy_perform(session);
	fclose(file);

	frame = cvLoadImage(filename);

	curl_easy_cleanup(session);
	curl_global_cleanup();

	return frame;
}

bool isYellow(IplImage* src, CvPoint origin, int heightROI, int widthROI, int thPercent){

	bool isYellow = yellowDetection(src, hsv_min_g, hsv_max_g, hsv_min2_g, hsv_max2_g, origin, heightROI, widthROI, thPercent);

	return isYellow;
}
